package com.hldsmart.smartda.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.base.BaseActivity;
import com.hldsmart.smartda.fragment.HomeFragment;
import com.hldsmart.smartda.fragment.MineFragment;
import com.hldsmart.smartda.fragment.ShopFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzh194 on 2017/1/16.
 * 应用主activity页面
 * 测试测试
 */

public class MainActivity extends BaseActivity {

    @Bind(R.id.viewpager)
    ViewPager viewPager;
    @Bind({R.id.tv_home, R.id.tv_shop, R.id.tv_mine})
    TextView[] tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //全屏显示，但状态栏不会被隐藏
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  //隐藏虚拟按键(导航栏)，有些手机会用虚拟按键来代替物理按键。
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.argb(70, 100, 100 ,100));
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        tv[0].setSelected(true);
        tv[0].setTextColor(getResources().getColor(R.color.colorAccent));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (TextView textView : tv) {
                    textView.setSelected(false);
                    textView.setTextColor(getResources().getColor(R.color.black));
                }
//                tv_title.setText(tv[position].getText());
                tv[position].setSelected(true);
                tv[position].setTextColor(getResources().getColor(R.color.colorAccent));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(0);

    }

    @OnClick({R.id.tv_home, R.id.tv_shop, R.id.tv_mine})
    public void onClick(View view) {
        for (int i = 0; i < tv.length; i++) {
            tv[i].setSelected(false);
            tv[i].setTextColor(getResources().getColor(R.color.black));
            tv[i].setTag(i);
        }
        view.setSelected(true);
        ((TextView) view).setTextColor(getResources().getColor(R.color.colorAccent));
        // 参数false代表瞬间切换，而不是平滑过渡
        viewPager.setCurrentItem((Integer) view.getTag(), false);
//        tv_title.setText(tv[(Integer) view.getTag()].getText());
    }


    private FragmentPagerAdapter fragmentAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new HomeFragment();
                case 1:
                    return new ShopFragment();
                case 2:
                    return new MineFragment();

            }
            return null;
        }
    };

}
